package controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

import modeles.News;
import modeles.Pages;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import robot.PMF;

@Controller
@RequestMapping("/contenuPage")
public class ContenuPagesVisiteursController {

	private static final Logger log = Logger.getLogger(MembresController.class.getName());
	private final String contact = "contact";
	private final String realisation = "realisation";
	private final String evenement = "evenement";
	private final String documentation = "documentation";
	private final String index = "index";
	private final String news = "news";
	
	@RequestMapping(value=contact,method=RequestMethod.GET)
	public ModelAndView getContact(){
		log.info("entrer dans le controller pages:getContact");
		Map<String, Object> model = new HashMap<String, Object>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Pages pageCourant = null;
		try{
        		pageCourant = pm.getObjectById(Pages.class,contact);
		}catch(JDOObjectNotFoundException e){
        		pageCourant = new Pages("", contact);
        }
		
		model.put("pageCourante",pageCourant);
		return new ModelAndView(contact,model);
	}
	
	@RequestMapping(value=documentation,method=RequestMethod.GET)
	public ModelAndView getDocumentation(){
		log.info("entrer dans le controller pages:getContact");
		Map<String, Object> model = new HashMap<String, Object>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Pages pageCourant = null;
		try{
        		pageCourant = pm.getObjectById(Pages.class,documentation);
		}catch(JDOObjectNotFoundException e){
        		pageCourant = new Pages("", documentation);
        }
		
		model.put("pageCourante",pageCourant);
		return new ModelAndView(documentation,model);
	}
	
	@RequestMapping(value=evenement,method=RequestMethod.GET)
	public ModelAndView getEvenement(){
		log.info("entrer dans le controller pages:getContact");
		Map<String, Object> model = new HashMap<String, Object>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Pages pageCourant = null;
		try{
        		pageCourant = pm.getObjectById(Pages.class,evenement);
		}catch(JDOObjectNotFoundException e){
        		pageCourant = new Pages("", evenement);
        }
		
		model.put("pageCourante",pageCourant);
		return new ModelAndView(evenement,model);
	}
	
	@RequestMapping(value=index,method=RequestMethod.GET)
	public ModelAndView getIndex(){
		log.info("entrer dans le controller pages:getIndex");
		Map<String, Object> model = new HashMap<String, Object>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Pages pageCourant = null;
		try{
        		pageCourant = pm.getObjectById(Pages.class,index);
		}catch(JDOObjectNotFoundException e){
        		pageCourant = new Pages("", index);
        }
		String query = "select from " + News.class.getName() + " order by dateDeMiseEnLigne desc" ;
		@SuppressWarnings("unchecked")
   		List<News> listeOfNews = (List<News>) pm.newQuery(query).execute();
		model.put("lastNews", listeOfNews.get(0));
		model.put("pageCourante",pageCourant);
		return new ModelAndView(index,model);
	}
	
	@RequestMapping(value=news,method=RequestMethod.GET)
	public ModelAndView getNews(){
		log.info("entrer dans le controller pages:getNews");
		Map<String, Object> model = new HashMap<String, Object>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + News.class.getName() + " order by dateDeMiseEnLigne desc" ;
		
		@SuppressWarnings("unchecked")
   		List<News> listeOfNews = (List<News>) pm.newQuery(query).execute();
		
		model.put("listeNews", listeOfNews);
		return new ModelAndView(news,model);
	}
	
	@RequestMapping(value=realisation,method=RequestMethod.GET)
	public ModelAndView getRealisation(){
		log.info("entrer dans le controller pages:getContact");
		Map<String, Object> model = new HashMap<String, Object>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Pages pageCourant = null;
		try{
        		pageCourant = pm.getObjectById(Pages.class,realisation);
		}catch(JDOObjectNotFoundException e){
        		pageCourant = new Pages("", realisation);
        }
		
		model.put("pageCourante",pageCourant);
		return new ModelAndView(realisation,model);
	}
	
	@InitBinder
	public void initLogin(WebDataBinder binder) {
		binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
	}
}
